<?php
/**
 * Created by PhpStorm.
 * User: Sinri
 * Date: 2017/10/5
 * Time: 13:13
 */

namespace sinri\wenli;


class DutyForKyodo extends PageReader
{
    public function duty()
    {
        $bookNameDict = [
            "創世記",
            "出エジプト記",
            "レビ記",
            "民数記",
            "申命記",
            "ヨシュア記",
            "士師記",
            "ルツ記",
            "サムエル記上",
            "サムエル記下",
            "列王紀上",
            "列王紀下",
            "歴代志上",
            "歴代志下",
            "エズラ記",
            "ネヘミヤ記",
            "エステル記",
            "ヨブ記",
            "詩篇",
            "箴言",
            "コヘレトの言葉",
            "雅歌",
            "イザヤ書",
            "エレミヤ書",
            "哀歌",
            "エゼキエル書",
            "ダニエル書",
            "ホセア書",
            "ヨエル書",
            "アモス書",
            "オバデヤ書",
            "ヨナ書",
            "ミカ書",
            "ナホム書",
            "ハバクク書",
            "ゼパニヤ書",
            "ハガイ書",
            "ゼカリヤ書",
            "マラキ書",
            "マタイによる福音書",
            "マルコによる福音書",
            "ルカによる福音書",
            "ヨハネによる福音書",
            "使徒言行録",
            "ローマの信徒への手紙",
            "コリントの信徒への手紙一",
            "コリントの信徒への手紙二",
            "ガラテヤの信徒への手紙",
            "エペソの信徒への手紙",
            "ピリピの信徒への手紙",
            "コロサイの信徒への手紙",
            "テサロニケの信徒への手紙一",
            "テサロニケの信徒への手紙二",
            "テモテヘの手紙一",
            "テモテヘの手紙二",
            "テトスヘの手紙",
            "ピレモンヘの手紙",
            "ヘブル人への手紙",
            "ヤコブの手紙",
            "ペテロの手紙一",
            "ペテロの手紙二",
            "ヨハネの手紙一",
            "ヨハネの手紙二",
            "ヨハネの手紙三",
            "ユダの手紙",
            "ヨハネの黙示録",
        ];
        for ($i = 0; $i < 66; $i++) {
            $chapter_id = $i + 1;
            if ($chapter_id < 10) $chapter_id = "0" . $chapter_id;
            $chapter_name = $bookNameDict[$i];
            $chapter_count = $this->bookChapterMapping($i) . "章";
            $this->dutyForBook($chapter_id, $chapter_name, $chapter_count);
        }
    }

    public function version()
    {
        return "kyodo";
    }

    protected function getBookURL($book_id, $book_name = null, $chapter_id = null)
    {
        $book_id = intval($book_id, 10);
        if ($book_id < 10) $book_id = "0" . $book_id;
        return "http://www.godcom.net/japan/" . $book_id . ".html";
    }

    protected function html2txt($html, $chapter = null)
    {
        $html = iconv("Shift-JIS", "UTF-8//IGNORE", $html);

        preg_match_all('/\d+:\d+[ 　].+(?=\<[Bb][Rr]\>)/', $html, $matches);

        $chapters = [];
        foreach ($matches[0] as $match) {
            //var_dump($matches);
            $parts = explode(':', $match);
            if (!isset($chapters[$parts[0]])) {
                $chapters[$parts[0]] = '';
            }
            $chapters[$parts[0]] .= $match . PHP_EOL;
        }
        return $chapters;
    }
}

//聖書  新共同訳：
//(c)共同訳聖書実行委員会
//Executive Committee of The Common Bible Translation
//(c)日本聖書協会
//Japan Bible Society , Tokyo 1987,1988